package com.softserveinc.ita.kaiji.rest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.softserveinc.ita.kaiji.dto.GameInfoDto;
import com.softserveinc.ita.kaiji.dto.rest.RestGameInfoDto;
import com.softserveinc.ita.kaiji.model.player.bot.Bot;
import com.softserveinc.ita.kaiji.service.GameService;



/**
 * Creates new game with REST. GET-request creates default game-settings object.
 * POST-request creates final game-settings object, which can be passed to RestPlayGameController.
 * 
 * @author Boiko Eduard
 * @version 1.1
 * @since 1.04.14 
 */


@RestController
@RequestMapping(value="/rest/create-training", produces="application/json")
public class RestCreateTrainingWithBotController {
	
    private static Logger logger = Logger.getLogger(RestCreateTrainingWithBotController.class);

    @Autowired
    private GameService gameService;
    /*
     *  Example of JSON client will receive:
     *  {"gameName":"Training","playerName":"Batman","botGame":true,"numberOfCards":3,"botType":"MEDIUM"}
     */
    @RequestMapping(method = RequestMethod.GET)
    public GameInfoDto createRestGameWithDefaultSettings(){
        
    	GameInfoDto gameInfoDto = new GameInfoDto();
    	gameInfoDto.setGameName("Training");
    	gameInfoDto.setPlayerName("Batman");
    	gameInfoDto.setNumberOfCards(3);
    	gameInfoDto.setBotType(Bot.Types.MEDIUM);
        
		if (logger.isTraceEnabled()){
			logger.trace("Default GameInfoDto created. Passing it to REST user");}
        
        return gameInfoDto;
    }
    
    /*
     *  Example of JSON client will receive:
     *  {"gameInfoDto":{"gameName":"Training","playerName":"Batman","botGame":true,"numberOfCards":3,"botType":"MEDIUM"},"playerId":0,"gameId":0}
     */
    @RequestMapping(method = RequestMethod.POST, consumes="application/json")
    public RestGameInfoDto createRestGameWithCustomSettings(@RequestBody GameInfoDto gameInfoDto){

    	RestGameInfoDto restGameInfoDto = new RestGameInfoDto();

    	Integer gameId   = gameService.createGame(gameInfoDto);
    	restGameInfoDto.setGameId(gameId);
        
		if (logger.isTraceEnabled()){
			logger.trace("REST training with bot created. Passed gameId and playerId");}
		
        return restGameInfoDto;
    }
    
}
